package com.web;

import com.pojo.User;
import com.service.UserService;
import com.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Discription:
 */
public class RegistServlet extends HttpServlet {
    UserService userService=new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        if("1234".equalsIgnoreCase(code)){
            if (userService.exitsUsername(username)){
                System.out.println("用户名["+username+"]已存在");
                req.getRequestDispatcher("pages/User/regist.html").forward(req,resp);
            }else{
                userService.registUser(new User(null,username,password,email));
                req.getRequestDispatcher("pages/User/regist_success.html").forward(req,resp);
            }
        }else{
            System.out.println("验证码["+code+"]错误");
            req.getRequestDispatcher("pages/User/regist.html").forward(req,resp);
        }
    }
}
